package com.nf.health.app.activity.home;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.home.fragment.BookingRegisteredFragment;
import com.nf.health.app.activity.home.fragment.DoctorDetailsFragment;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.http.Act;
import com.nf.health.app.inquiry.bean.DoctorDetailBean;
import com.nf.health.app.utils.ImageLoaderUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;



public class DoctorDetailsActivity extends BaseActivity {

    private String mDoctorId = "";
    private DoctorDetailsFragment mDetailsFragment;
    private BookingRegisteredFragment mRegisteredFragment;
    @Bind(R.id.titlebar_title_tv)
    TextView mTitlebarTitleTv;
    @Bind(R.id.tabLayout)
    TabLayout mTabLayout;
    @Bind(R.id.viewpager)
    ViewPager mViewPager;


    @Bind(R.id.iv_head_icon_doc)
    CircleImageView mIvHeadIconDoc;
    @Bind(R.id.tv_attention)
    TextView mTvAttention;
    @Bind(R.id.tv_doctor_name)
    TextView mTvDoctorName;
    @Bind(R.id.tv_hopital)
    TextView mTvHopital;
    @Bind(R.id.tv_department)
    TextView mTvDepartment;
    @Bind(R.id.tv_doctor_post)
    TextView mTvDoctorPost;
    private String mAttention = "";
    private String from = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_new_doctor_details);
        ButterKnife.bind(this);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mTvAttention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //   -1 不能关注, 0未关注，1已关注，2 不能取消关注
                if (mAttention.equals("0")) {//去关注
                    provider.addAttention(mDoctorId);
                } else if (mAttention.equals("1")) {//取消关注
                    provider.requestCancleFoucsDoctor(mDoctorId, Act.Flag.ATTATION_DELETE_DOCTOR_FLG);
                } else if (mAttention.equals("-1")) {//不能关注
                    provider.requestCancleFoucsDoctor(mDoctorId, Act.Flag.ATTATION_DELETE_DOCTOR_FLG);
                    // GetToast.useString(DocDetailActivity.this, "不能关注");
                } else if (mAttention.equals("2")) {//不能取消关注
                    // GetToast.useString(DocDetailActivity.this, "不能取消关注");
                    provider.requestCancleFoucsDoctor(mDoctorId, Act.Flag.ATTATION_DELETE_DOCTOR_FLG);
                }
            }
        });
    }

    private void initView() {
        mDetailsFragment = new DoctorDetailsFragment();
        mRegisteredFragment = new BookingRegisteredFragment();
        mDoctorId = getIntent().getStringExtra(Constants.DOCTOR_ID);
        from = getIntent().getStringExtra(Constants.FROM);
        mTitlebarTitleTv.setText("医生详情");
        MyViewPagerAdapter viewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
        viewPagerAdapter.addFragment(mDetailsFragment, "医生详情");
        viewPagerAdapter.addFragment(mRegisteredFragment, "预约挂号");
        mViewPager.setAdapter(viewPagerAdapter);//设置适配器
        mTabLayout.setupWithViewPager(mViewPager);

        if("registered".equals(from)){
            mViewPager.setCurrentItem(1);
        }
    }

    private void initData() {
        provider.requestDoctorDetail2(mDoctorId);//请求医生 详情

    }
    public interface OpFragment{
        void onAction(String goodAt, String content, String doctorId);
    }
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);

        if (Act.Flag.DOC_DETAIL.equals(actionName)) {
            DoctorDetailBean doctorDetail = (DoctorDetailBean) object;
            setDataToView(doctorDetail);
            if(doctorDetail != null) {
                mDetailsFragment.onAction(doctorDetail.getGoodremedy(),doctorDetail.getExperience(),mDoctorId);
                mRegisteredFragment.onAction(doctorDetail.getGoodremedy(),doctorDetail.getExperience(),mDoctorId);
            }
        }

        if(Act.Flag.ATTATION_DOCTOR_FLG.equals(actionName)){//去关注医生
            provider.requestDoctorDetail2(mDoctorId);//请求接口去刷新
        }

        if(Act.Flag.ATTATION_DELETE_DOCTOR_FLG.equals(actionName)){
            provider.requestDoctorDetail2(mDoctorId);//请求接口去刷新
        }
    }

    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
    }

    private void setDataToView(DoctorDetailBean bean){
        if(bean != null) {
            ImageLoaderUtils.getinstance(this).getImage(mIvHeadIconDoc, bean.getDoctorhimg(), R.drawable.default_head);
            mTvDoctorName.setText(bean.getDoctorname());
            mTvHopital.setText(bean.getHospitalname());
            mTvDepartment.setText(bean.getDepartment());
            mTvDoctorPost.setText(bean.getProfessional());

            mAttention = (bean.getAttention() == null) ? "0" : bean.getAttention();
            //   -1 不能关注, 0未关注，1已关注，2 不能取消关注
            if (mAttention.equals("0")) {
                mTvAttention.setText("关注");
            } else if (mAttention.equals("1")) {
                mTvAttention.setText("取消关注");
            } else if (mAttention.equals("-1")) {
                mTvAttention.setText("关注");
            } else if (mAttention.equals("2")) {
                mTvAttention.setText("取消关注");
            }
        }
    }

    public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();//添加的Fragment的集合
        private final List<String> mFragmentsTitles = new ArrayList<>();//每个Fragment对应的title的集合

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * @param fragment      添加Fragment
         * @param fragmentTitle Fragment的标题，即TabLayout中对应Tab的标题
         */
        public void addFragment(Fragment fragment, String fragmentTitle) {
            mFragments.add(fragment);
            mFragmentsTitles.add(fragmentTitle);
        }

        @Override
        public Fragment getItem(int position) {
            //得到对应position的Fragment
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            //返回Fragment的数量
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            //得到对应position的Fragment的title
            return mFragmentsTitles.get(position);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
